﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class CongnoKhList : System.Web.UI.Page
    {
        CHUCNANG objchucnang = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            KiemtraQUyenSuDung(); 
            if (!IsPostBack)
            {
                LoadHienthi();
            }
        }
        public void LoadHienthi()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CONGNO_KHRepository repository = new CONGNO_KHRepository(new EFRepository<CONGNO_KH>(), iunitfwork);
            IQueryable<CONGNO_KH> objCongnokh = repository.All();
            Grcongnokh.DataSource = objCongnokh;
            Grcongnokh.DataBind();
        }

        protected void Grcongnokh_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbtnsua")
            {
                Response.Redirect("CongnoKhAdd.aspx?ID=" + e.CommandArgument);
            }
            if (e.CommandName == "lbtnxoa")
            {
                try
                {
                    IUnitOfWork iunitfwork = new EFUnitOfWork();
                    CONGNO_KHRepository repository = new CONGNO_KHRepository(new EFRepository<CONGNO_KH>(), iunitfwork);
                    CONGNO_KH objcongno = repository.GetById(int.Parse(e.CommandArgument.ToString()));
                    repository.Delete(objcongno);
                    repository.Save();
                    LoadHienthi();
                }
                catch (Exception ex)
                {
                    RegisterClientScriptBlock("Thông báo", "<script> alert('Không thể xóa được')</script>");
                }
            }
        }

        public void KiemtraQUyenSuDung()
        {
            lbtnthemoi.Visible = false;
            if (Session["MANV"] != null)
            {
                string strMANV = Session["MANV"].ToString();
                IUnitOfWork iunitwork = new EFUnitOfWork();
                lbtnthemoi.Visible = false;
                NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitwork);
                NHANVIEN objNhanvien = repository.All().Where(p => p.MANV == strMANV).FirstOrDefault();
                if (objNhanvien != null)
                {
                    objchucnang = Commomcs.QuyenTruyCapChucnang(Request.Url.AbsolutePath, objNhanvien.MANV);
                    if (objchucnang != null)
                    {
                        if (objchucnang.ISADD == true)
                        {
                            lbtnthemoi.Visible = true;
                        }
                        else
                        {
                            lbtnthemoi.Visible = false;
                        }

                    }
                }
            }
        }
        protected void Grcongnokh_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0) return;
            CONGNO_KH objcongno = ((CONGNO_KH)e.Row.DataItem);
            if (objcongno != null)
            {
                Label lbkhachhang = ((Label)e.Row.FindControl("lbkhachhang"));
                if (lbkhachhang != null)
                {
                    lbkhachhang.Text = objcongno.KHACHHANG.TENKH;
                }
                Label lbtinhtrang = ((Label)e.Row.FindControl("lbtinhtrang"));
                if(lbtinhtrang !=null)
                {
                    if (objcongno.TINHTRANG == true)
                    {
                        lbtinhtrang.Text = "Đã thanh toán";
                    }
                    else
                    {
                        lbtinhtrang.Text = "Chưa thanh toán";
                    }
                }
            }
            LinkButton lbtnsua = ((LinkButton)e.Row.FindControl("lbtnsua"));
            LinkButton lbtnxoa = ((LinkButton)e.Row.FindControl("lbtnxoa"));
            //lbtnsua.Visible = false;
            //lbtnxoa.Visible = false;
            //if (objchucnang != null)
            //{
               
            //    if (lbtnsua != null)
            //    {
            //        if (objchucnang.ISEDIT == true)
            //        {
            //            lbtnsua.Visible = true;
            //        }
            //        else
            //        {
            //            lbtnsua.Visible = false;
            //        }
            //    }
            //    if (lbtnxoa != null)
            //    {
            //        if (objchucnang.ISDELETE == true)
            //        {
            //            lbtnsua.Visible = true;
            //        }
            //        else
            //        {
            //            lbtnxoa.Visible = false;
            //        }
            //    }
            //}

        }

        protected void Grcongnokh_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            LoadHienthi();
            Grcongnokh.PageIndex = e.NewSelectedIndex;
            Grcongnokh.DataBind();
        }
    }
}